package controllers

import (
	"Myshop/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/gomodule/redigo/redis"
	"strconv"
)

type CartController struct {
	beego.Controller
}

func (this *CartController) HandleAddCart() {
	skuid, err := this.GetInt("skuid")
	count, err2 := this.GetInt("count")
	resp := make(map[string]interface{})
	defer this.ServeJSON()

	if err != nil || err2 != nil {
		resp["code"] = 1
		resp["msg"] = "传递的数据不正确"
		this.Data["json"] = resp
		return
	}

	userName := GetUserName(&this.Controller)
	if userName == "" {
		resp["code"] = 2
		resp["msg"] = "当前用户未登录"
		this.Data["json"] = resp
		return
	}

	o := orm.NewOrm()
	var user models.User
	user.Name = userName
	o.Read(&user, "Name")

	conn, err1 := redis.Dial("tcp", ":6379")
	if err1 != nil {
		beego.Error("redis数据库链接错误")
	}
	gC, err := conn.Do("hget", "cart_"+strconv.Itoa(user.Id), skuid)
	preCount, _ := redis.Int(gC, err)

	conn.Do("hset", "cart_"+strconv.Itoa(user.Id), skuid, count+preCount)
	defer conn.Close()
	allC, err := conn.Do("hgetall", "cart_"+strconv.Itoa(user.Id))

	//	回复助手函数
	allCount, err := redis.IntMap(allC, err)

	var cartCount = 0
	for _, value := range allCount {
		//num, _ := strconv.Atoi(value)
		cartCount += value
	}

	resp["code"] = 5
	resp["msg"] = "ok"
	resp["cartCount"] = cartCount
	this.Data["json"] = resp

}

func GetCartCount(this *beego.Controller) int {
	//	获取redis中购物车的数量
	userName := GetUserName(this)

	if userName == "" {
		return 0
	}
	o := orm.NewOrm()
	var user models.User
	user.Name = userName
	o.Read(&user, "Name")

	conn, err := redis.Dial("tcp", ":6379")
	if err != nil {
		return 0
	}

	defer conn.Close()

	allC, err := conn.Do("hgetall", "cart_"+strconv.Itoa(user.Id))

	//	回复助手函数
	allCount, err := redis.IntMap(allC, err)
	var cartCount = 0
	for _, value := range allCount {
		//num, _ := strconv.Atoi(value)
		cartCount += value
	}

	return cartCount
}
func (this *CartController) ShowCart() {
	user := GetUserName(&this.Controller)
	conn, err := redis.Dial("tcp", ":6379")
	if err != nil {
		beego.Info("链接失败")
	}
	defer conn.Close()

	o := orm.NewOrm()
	var userM models.User
	userM.Name = user
	o.Read(&userM, "Name")

	gM, err := conn.Do("hgetall", "cart_"+strconv.Itoa(userM.Id))

	goodsMap, err := redis.IntMap(gM, err)
	goods := make([]map[string]interface{}, len(goodsMap))

	index := 0
	totalPrice := 0.0
	total := 0
	for i, value := range goodsMap {
		goodsid, _ := strconv.Atoi(i)
		var goodSku models.GoodsSKU
		goodSku.Id = goodsid
		o.Read(&goodSku)

		tmp := make(map[string]interface{})
		tmp["goodsSKU"] = goodSku
		tmp["count"] = value
		tmp["addCount"] = goodSku.Price * value
		totalPrice += float64(goodSku.Price * value)
		total += value
		goods[index] = tmp
		index += 1
	}

	this.Data["totalPrice"] = totalPrice
	this.Data["totalCount"] = total
	this.Data["goods"] = goods
	this.TplName = "cart.html"
}

func (this *CartController) UpdateCart() {
	skuid, err1 := this.GetInt("skuid")
	count, err2 := this.GetInt("count")
	resp := make(map[string]interface{})
	defer this.ServeJSON()

	if err1 != nil || err2 != nil {
		resp["code"] = 1
		resp["errmsg"] = "请求数据不正确"
		beego.Info("请求数据不正确")
		return
	}

	userName := GetUserName(&this.Controller)
	if userName == "nil" {
		resp["code"] = 3
		resp["errmsg"] = "当前用户未登录"
		this.Data["json"] = resp
		return
	}

	var user models.User
	o := orm.NewOrm()
	user.Name = userName
	o.Read(&user, "Name")

	conn, err := redis.Dial("tcp", ":6379")
	if err != nil {
		resp["code"] = 2
		resp["errmsg"] = "redis链接失败"
		this.Data["json"] = resp
		return
	}
	defer conn.Close()
	conn.Do("hset", "cart_"+strconv.Itoa(user.Id), skuid, count)

	resp["code"] = 5
	resp["msg"] = "ok"
	this.Data["json"] = resp
}

func (this *CartController) DeleteCart() {
	skuid, err := this.GetInt("skuid")
	resp := make(map[string]interface{})
	defer this.ServeJSON()

	if err != nil {
		resp["code"] = 1
		resp["errmsg"] = "请求数据不正确"
		beego.Info("请求数据不正确")
		return
	}
	userName := GetUserName(&this.Controller)
	if userName == "nil" {
		resp["code"] = 3
		resp["errmsg"] = "当前用户未登录"
		this.Data["json"] = resp
		return
	}

	var user models.User
	o := orm.NewOrm()
	user.Name = userName
	o.Read(&user, "Name")

	conn, err := redis.Dial("tcp", ":6379")
	if err != nil {
		resp["code"] = 2
		resp["errmsg"] = "redis链接失败"
		this.Data["json"] = resp
		return
	}
	defer conn.Close()
	//redis---》删除哈希 : hdel
	conn.Do("hdel", "cart_"+strconv.Itoa(user.Id), skuid)
	resp["code"] = 5
	resp["msg"] = "ok"
	this.Data["json"] = resp
}
